home *** CD-ROM | disk | FTP | other *** search
-
- /**
-
- # # # #
- ## ## # # # ##### # # # ###### ##### #### ######
- # # # # # # # # # # # # # # # #
- # # # # # # # # # # ##### # # #### #####
- # # # # # # # # # # ##### # #
- # # # # # # # # # # # # # # #
- # # #### ###### # # # ###### # # #### ######
-
- #####
- # # # # #### ##### ###### # #
- # # # # # # ## ##
- ##### # #### # ##### # ## #
- # # # # # # #
- # # # # # # # # #
- ##### # #### # ###### # #
-
- Copyright: Robert Grant, 1993
-
- **/
-
- #define _XOPEN_SOURCE
- #define _ALL_SOURCE
-
- /* includes */
-
- #include <stdio.h>
- #include <string.h>
- #include <X11/Xlib.h>
- #include <X11/keysym.h>
- #include <math.h>
- #include <stdlib.h>
-
- #include <graphics.h>
- #include <messages.h>
- #include <eventHandler.h>
- #include <clientManager.h>
- #include <objectManager.h>
- #include <userInterface.h>
-
- #include <ext_biwing.h>
-
- /* Globals */
-
- /* Macros */
-
- int GetButtonPress(int mask, int *speed) {
-
- if (mask & Button2Mask)
- return(TRUE);
- if ((mask & Button1Mask) && (*speed < MAXSPEED))
- *speed += 1;
- if ((mask & Button3Mask) && (*speed > MINSPEED))
- *speed -= 1;
- return(FALSE);
- }
-
- void regenerateShield(int *shield) {
- static int regen = 0;
- if (*shield < 40)
- regen++;
- if (regen == 50 && *shield < 40) {
- (*shield)++;
- regen = 0;
- }
- }
-
- void moveBiwing(object_t *theBiwing, biwingExt_t *biwingData,
- move_t *event) {
-
- serverEventMsg_t ev;
- changePart_t part;
- changePanel_t panel;
- moveEvent_t move;
- int speed = biwingData->speed;
- int shields = biwingData->shields;
- double temp = biwingData->temp;
- memset(&ev, 0, sizeof(serverEventMsg_t));
- memset(&part, 0, sizeof(changePart_t));
- theBiwing->coords.loc.x -= sin(RADIANS(theBiwing->coords.rot.y)) * speed/8.0;
- theBiwing->coords.loc.y -= sin(RADIANS(theBiwing->coords.rot.x)) * speed/8.0;
- theBiwing->coords.loc.z += cos(RADIANS(theBiwing->coords.rot.y)) * speed/8.0;
-
- regenerateShield(&biwingData->shields);
- biwingData->temp += ((biwingData->speed - 30) / 10.0);
- if (biwingData->temp < 0) biwingData->temp = 0;
- if (biwingData->temp > 40) biwingData->temp = 40;
- if (biwingData->temp == 40)
- biwingData->speed -= 2;
- else if (biwingData->temp > 35)
- biwingData->speed -= 1;
- if (speed != biwingData->speed) {
- strcpy(part.name, "biwing:biwing_leftwing");
- part.attrib = PART_ROT;
- part.coords.rot.z = (3 * biwingData->speed);
- composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name, &part);
- broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
- strcpy(part.name, "biwing:biwing_rightwing");
- part.coords.rot.z = -(3 * biwingData->speed);
- composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name, &part);
- broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
- strcpy(panel.name, "biwing:speed");
- panel.type = PANEL_VALUE;
- panel.value = biwingData->speed;
- composeServerEventMessage(&ev, CHANGE, CHG_PANEL, theBiwing->type, &panel);
- sendMessage(theBiwing->socket, SERVEREVENT, sizeof(serverEventMsg_t), &ev);
- }
- if (temp != biwingData->temp) {
- panel.type = PANEL_VALUE;
- strcpy(panel.name, "biwing:temp");
- panel.value = biwingData->temp;
- composeServerEventMessage(&ev, CHANGE, CHG_PANEL, theBiwing->type, &panel);
- sendMessage(theBiwing->socket, SERVEREVENT, sizeof(serverEventMsg_t), &ev);
- }
- if (shields != biwingData->shields) {
- panel.type = PANEL_VALUE;
- strcpy(panel.name, "biwing:shield");
- panel.value = biwingData->shields;
- composeServerEventMessage(&ev, CHANGE, CHG_PANEL, theBiwing->type, &panel);
- sendMessage(theBiwing->socket, SERVEREVENT, sizeof(serverEventMsg_t), &ev);
- }
-
- strcpy(move.name, theBiwing->name);
- memcpy(&move.coords, &theBiwing->coords, sizeof(viewpoint));
- composeServerEventMessage(&ev, MOVE, 0, NULL, &move);
- broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
- moveObjects(biwingData->missile);
- moveObjects(biwingData->mine);
- }
-
- int calcVectors(viewpoint *the_ship, int x, int y,int speed) {
-
- double divisor = speed;
- if (divisor == 0) divisor = 1;
- the_ship->rot.y -= (x /100.0);
- the_ship->rot.x = speed * y/170.0;
- the_ship->rot.z = -x/20.0;
- if (the_ship->rot.y > 359) the_ship->rot.y -= 360;
- if (the_ship->rot.y < 0) the_ship->rot.y += 360;
- }
-
- int checkBiwingCollision(object_t *theBiwing, biwingExt_t *biwingData,
- collideEvent_t *event) {
-
- object_t *theCollider;
- serverEventMsg_t ev;
- changePanel_t panel;
- int shields;
- theCollider = event->obj;
- if (theCollider->parent == theBiwing)
- return 0;
- if (!strcmp(theCollider->type, "missile") ||
- !strcmp(theCollider->type, "mine")) {
- biwingData->counter = 0;
- if (biwingData->shields > 0)
- biwingData->shields -= 25;
- if (biwingData->shields < 0) {
- biwingData->state = HIT;
- biwingData->shields = 0;
- }
- panel.type = PANEL_VALUE;
- strcpy(panel.name, "biwing:shield");
- panel.value = biwingData->shields;
- composeServerEventMessage(&ev, CHANGE, CHG_PANEL, theBiwing->type, &panel);
- sendMessage(theBiwing->socket, SERVEREVENT, sizeof(serverEventMsg_t), &ev);
- return 1;
- }
- return 0;
- }
-
- int updateBiwingScore(object_t *theBiwing, biwingExt_t *biwingData,
- termEvent_t *event) {
-
- serverEventMsg_t ev;
- changePanel_t panel;
-
- biwingData->score += 100;
- sprintf(panel.label, "%05d", biwingData->score);
- strcpy(panel.name, "biwing:score");
- panel.type = PANEL_LABEL;
- composeServerEventMessage(&ev, CHANGE, CHG_PANEL, "biwing", &panel);
- sendMessage(theBiwing->socket, SERVEREVENT, sizeof(ev), &ev);
- }
-
- int flyBiwing(object_t *theBiwing, biwingExt_t *biwingData,
- clientEventMsg_t *event) {
-
- int FireButton;
- serverEventMsg_t ev;
- changePart_t part;
- changePanel_t panel;
- int speed = biwingData->speed;
- memset(&ev, 0, sizeof(serverEventMsg_t));
- memset(&part, 0, sizeof(changePart_t));
- switch (biwingData->state) {
- case FLYING:
- FireButton = GetButtonPress(event->buttons, &biwingData->speed);
- calcVectors(&(theBiwing->coords), event->x, event->y, biwingData->speed);
- if (speed != biwingData->speed) {
- strcpy(part.name, "biwing:biwing_leftwing");
- part.attrib = PART_ROT;
- part.coords.rot.z = (3 * biwingData->speed);
- composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name,
- &part);
- broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
- strcpy(part.name, "biwing:biwing_rightwing");
- part.coords.rot.z = -(3 * biwingData->speed);
- composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name,
- &part);
- broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
- panel.type = PANEL_VALUE;
- strcpy(panel.name, "biwing:speed");
- panel.value = biwingData->speed;
- composeServerEventMessage(&ev, CHANGE, CHG_PANEL, theBiwing->type,
- &panel);
- sendMessage(theBiwing->socket, SERVEREVENT, sizeof(serverEventMsg_t),
- &ev);
- }
- if (FireButton)
- initSubObject(theBiwing, biwingData->missile);
- if (event->keys == XK_m)
- initSubObject(theBiwing, biwingData->mine);
- break;
- case HIT:
- if (biwingData->counter < 100) {
- biwingData->counter++;
- memset(&ev, 0, sizeof(serverEventMsg_t));
- strcpy(part.name, "biwing:biwing_leftwing");
- part.attrib = PART_ROT | PART_LOC;
- part.coords.rot.z = (5 * biwingData->counter);
- part.coords.loc.x = -10 + -(5 * biwingData->counter);
- part.coords.loc.y = -10;
- composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name,
- &part);
- broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
- strcpy(part.name, "biwing:biwing_rightwing");
- part.coords.rot.z = -(5 * biwingData->counter);
- part.coords.loc.x = 10 + (5 * biwingData->counter);
- composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name,
- &part);
- broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
- }
- else {
- biwingData->state = FLYING;
- memset(&part, 0, sizeof(changePart_t));
- strcpy(part.name, "biwing:biwing_leftwing");
- part.attrib = PART_ROT | PART_LOC;
- part.coords.loc.x = -10;
- part.coords.loc.y = -10;
- composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name,
- &part);
- broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
- strcpy(part.name, "biwing:biwing_rightwing");
- part.coords.loc.x = 10;
- composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name,
- &part);
- broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
- }
- break;
- default:
- break;
- }
- }
-
- int addBiwingExtensions(object_t *theObject) {
-
- biwingExt_t *temp = NULL;
- int i;
- char name[50];
- if ((temp = (biwingExt_t *)malloc(sizeof(biwingExt_t))) == NULL) {
- fprintf(stderr, "Can't allocate space for biwing extension!\n");
- return 0;
- }
- temp->shields = 40;
- temp->speed = 0;
- temp->temp = 0;
- temp->state = FLYING;
- temp->counter = 100;
- temp->missile = NULL;
- temp->mine = NULL;
- temp->score = 0;
- addEventHandler(theObject, CLIENT_NOTIFY_EVENT, temp, flyBiwing);
- addEventHandler(theObject, MOVE_EVENT, temp, moveBiwing);
- addEventHandler(theObject, COLLISION_NOTIFY_EVENT, temp, checkBiwingCollision);
- addEventHandler(theObject, TERM_NOTIFY_EVENT, temp, updateBiwingScore);
- theObject->collider = createCollider();
- theObject->collider->obj = theObject;
- theObject->collider->type = OBJ_DYNAMIC;
- theObject->collider->boundType = BOX;
- theObject->collider->box[0].x = -10;
- theObject->collider->box[0].y = -10;
- theObject->collider->box[0].z = -20;
- theObject->collider->box[1].x = 10;
- theObject->collider->box[1].y = 10;
- theObject->collider->box[1].z = 20;
- addCollider(theObject->collider);
- for (i=0; i< MAX_MISSILES; i++) {
- sprintf(name, "%s-missile%1d\0", theObject->name, i);
- addObject(&temp->missile, createLocalObject(theObject, name, "missile", theObject->socket));
- }
- for (i=0; i< MAX_MINES; i++) {
- sprintf(name, "%s-mine%1d\0", theObject->name, i);
- addObject(&temp->mine, createLocalObject(theObject, name, "mine", theObject->socket));
- }
- }
-
- int installBiwingExtensions() {
-
- installEventHandlerCreator("biwing", addBiwingExtensions);
- installMissileExtensions();
- installMineExtensions();
- }
-
-